<?php
//微信用户相关

namespace App\Http\Controllers;

use App\Repositories\FollowRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use App\Traits\BaseHttp;
use App\Models\MonitorModel;
use App\Repositories\MonitorRepository;
use App\Repositories\OperateRepository;

class MonitorController extends BaseController
{
    use BaseHttp;
    /**
     * 获取一些通用的配置
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function index (Request $request) {
        $monitors = @MonitorModel::where('status', MonitorModel::ENABLED)
            ->select('stock_code', 'code_prefix')
            ->get();
        /*foreach ($monitors as $monitor) {
            $stockCode = $monitor['code_prefix'] . $monitor['stock_code'] . $monitor['aa'];
            if (strlen($stockCode) != 8) {
                $this->error('410', '错误代码');
            }
            dd($stockCode);
            $this->setHeaders([
                'Content-Type' => 'application/x-www-form-urlencoded'
            ]);
//        dd(env('JUHE_STOCK_KEY'));
            $rst = $this->httpGet('http://web.juhe.cn/finance/stock/hs', [
                'gid' => $stockCode,
//            'type' => 1,
                'key' => env('JUHE_STOCK_KEY')
            ]);
            return $this->success($rst);
        }*/
        $this->setHeaders([
            'Content-Type' => 'application/x-www-form-urlencoded'
        ]);
        $rst = $this->httpGet('http://web.juhe.cn/finance/stock/shall', [
            'page' => 1,
            'type' => 4,
            'key' => env('JUHE_STOCK_KEY')
        ]);
        return $this->success($rst);
    }

    /**
     * @param MonitorRepository $monitorRepository
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMonitorInfo (MonitorRepository $monitorRepository, Request $request)
    {
        $id = $request->get('id');
        $moniforInfo = @$monitorRepository->getMonitorById($id);
        if ($moniforInfo) {
            return $this->success($moniforInfo);
        } else {
            return $this->error(401, '系统错误，请稍后再试');
        }
    }

    /**
     * @param Request $request
     * @param MonitorRepository $monitorRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function save (Request $request, MonitorRepository $monitorRepository, FollowRepository $followRepository, OperateRepository $operateRepository)
    {
        $allParams = $request->all();
        $followId = $allParams['follow_id'] ?? 0;
        $operateId = $allParams['operate_id'] ?? 0;
        unset($allParams['follow_id']);
        unset($allParams['operate_id']);
        $allParams['trigger_times_per_day'] = 3;
        $monitorId = @$monitorRepository->save($allParams);
        if ($monitorId) {
            if ($followId) {
                // 关注的监控
                @$followRepository->updateBeMonitor($followId, $monitorId);
            }
            if ($operateId) {
                // 交易的监控
                @$operateRepository->updateBeMonitor($operateId, $monitorId);
            }
            return $this->success([]);
        }
        return $this->error(405, '保存失败，请稍后再试');
    }
}
